﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CommerceAccountProxy = Microsoft.Commerce.Proxy.AccountService;
using TestWebApplication.CommerceTestDataProvider;
using TestWebApplication.CommerceTransaction;
using System.IO;
using Microsoft.CTP.CommerceAPI.SDKSample.Client.Utils;
using Microsoft.CTP.CommerceAPI.Proxy.v201112;
using Microsoft.CTP.CommerceAPI.SDKSample.Client.ClientSetup;
using Microsoft.CTP.CommerceAPI.SDKSample.Client.Api;

namespace TestWebApplication
{
    public class MINTPurchaseHandler
    {
        public static bool PurchaseContent(CommerceAccountProxy.Identity identity,PurchaseContentViewModel model)
        {
            string wlid = identity.IdentityProperty[0].Value;
            string puid = identity.IdentityValue;

            string contentTypeName = model.ContentCatalog.contentName;

            var purchaseContext = new PurchaseContext { ComputeOnly = false, Timestamp = CTPClient.UtcNow() };

            var priceInfo = new PriceInfo { Amount = model.ContentCatalog.contentAmount, Currency = "USD", IsTaxIncluded = false, TaxCode = "Online" };

            var contentCatalogInfo = new ContentCatalogInfo
            {
                ContentCategory = model.ContentCatalog.ContentCategory,
                LicenseType = model.ContentCatalog.LicenseType,
                EntitlementType = model.ContentCatalog.EntitlementType,
                Quantity = model.ContentCatalog.Quantity
            };

            //string sku = CommonUtils.RandomSku();
            string sku = "BSS-00001";
            var revenueInfo = new RevenueInfo
            {
                RevenueSku = sku,
                RevenueAllocationPriceType=null,
                RevenueAllocationProperties = null
                //RevenueAllocationPriceType = RevenueAllocationPriceType.Percentage,
               /* RevenueAllocationProperties = new[] {
                    new RevenueAllocationProperty { Namespace = "Namespace", Sku = sku, SkuCategory = SkuCategory.Revenue, Value = 0.5M }
                }*/
            };

            PaymentMethod paymentMethod = null;
            if (model.PIType == "Mobile Phone")
            {
                paymentMethod = new MobilePhonePaymentMethod
                {
                    BillingToken = model.MobilePhonePaymentMethod.BillingToken,
                    MobileOperatorId = model.MobilePhonePaymentMethod.MobileOperatorId,
                    PhoneNumber = model.MobilePhonePaymentMethod.PhoneNumber
                };
            }
            else if (model.PIType == "Credit Card")
            {
                paymentMethod = new RegisteredPaymentMethod { PaymentMethodId = model.CreditCardPaymentMethodId };
            }

            try
            {
                ContentApi.PurchaseContent(puid, wlid, contentTypeName, purchaseContext, priceInfo, contentCatalogInfo, revenueInfo, paymentMethod);
            }
            catch (Exception e)
            {
                return false;
            }
            return true;
            
        }
    }
}